package com.cunshe.web.manage;

import com.cunshe.common.ResponseVO;
import com.cunshe.config.annotation.Login;
import com.cunshe.domain.Menu;
import com.cunshe.domain.ProductFeature;
import com.cunshe.domain.TestCase;
import com.cunshe.service.CaseService;
import com.cunshe.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "检测案例")
@Login
@RestController
@RequestMapping(value = "/manage/case")
public class CaseController {

    @Autowired
    private CaseService caseService;
    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "新增")
    @PostMapping(value = "insert")
    public ResponseVO insert(@RequestBody TestCase record) {
        caseService.insert(record);
        return ResponseVO.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "delete")
    public ResponseVO update(@RequestParam("id") Long id) {
        caseService.delete(id);
        return ResponseVO.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping(value = "update")
    public ResponseVO update(@RequestBody TestCase record) {
        caseService.update(record);
        return ResponseVO.success();
    }

    @ApiOperation(value = "查询列表")
    @PostMapping(value = "list")
    public ResponseVO<List<TestCase>> list(TestCase record) {
        List<TestCase> list = caseService.list(record);
        List<Menu> all = menuService.all();
        for (TestCase testCase : list) {
            Menu menu = all.stream()
                    .filter(var -> var.getId().equals(testCase.getMenuId()))
                    .findFirst()
                    .orElse(Menu.builder().build());
            testCase.setMenuName(menu.getTitle());
            testCase.setMenuNameEn(menu.getTitleEn());
        }

        return ResponseVO.success(list);
    }

}